<?php
if (!session_id()) {
    session_start();
}
$max = 3;
$ratio = 3;
$ratio_mobaolixi = array(3,17);

if(isset($_POST['execute'])){
	if($_POST['execute']==0){ // Bat dau tinh gio
		$_SESSION['timestart'] = time();
	}
	if($_POST['execute']==1){ // khoi tao mang item
		$count = 0;
		$item = array();
		for($i=0;$i<60;$i++){
			$item[$i] = array();
			for($j=0;$j<25;$j++){
				$random = rand(0,100);
				if($random<=$ratio&&$count<$max) {
					$item[$i][] = array(1,1);
					$count++;
				} else  $item[$i][] = array(1,0);
			}
		}
		$_SESSION['item'] = $item;
		echo json_encode($item);
		exit;
	}
	if($_POST['execute']==2){ // Luu diem
		if(time()-$_SESSION['timestart'] >= 90){
			echo json_encode(array('hack'=>1));
			exit;
		}
		if($_POST['baolixi'] > $max){
			echo json_encode(array('hack'=>1));
			exit;
		}
		if($_POST['score']>=700){
			echo json_encode(array('hack'=>1));
			exit;
		}else{
			$submit_item = $_POST['submit_item'];
			foreach($submit_item as $key=>$row){
				if(empty($row)) unset($submit_item[$key]);
			}
			if(count($submit_item)!=$_POST['score']){
				echo json_encode(array('hack'=>1));
				exit;
			}
			foreach($submit_item as $row){
				$tmp = explode('|', $row);
				if(count($tmp)!=3){
					echo json_encode(array('hack'=>1));
					exit;
				}
				foreach($_SESSION['item'] as $km=>$map){
					foreach($map as $ki=>$item){
						if($km==$tmp[0]&&$ki==$tmp[1]&&$item[1]!=$tmp[2]){
							echo json_encode(array('hack'=>1));
							exit;
						}
					}
				}
			}

			global $wpdb;
		 //    $tb = 'ap_painting_user' ;
		 //    $user = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE user_id = {$_SESSION['user_id']}");
		 //    if($_POST['score']>=$user->score){
			//     $data = array('score'=> $_POST['score'], 'currentdate'=> time());
			//     $wpdb->update($tb,$data,array( 'id' => $user->id ));
			// }

		    $tb = 'ap_painting_play' ;
		    $data = array(
		    	'user_id'=> $_SESSION['user_id'], 
		    	'score'=> $_POST['score'], 
		    	'adddate'=> time(), 
		    	'baolixi'=> $_POST['baolixi'], 
		    	'request'=> serialize($_POST),
		    	);
		    $wpdb->insert($tb,$data,array('%s','%s','%s','%s','%s'));
		    $_SESSION['baolixi'] = $_POST['baolixi'];
		    $_SESSION['mobaolixi'] = $wpdb->insert_id;

			echo json_encode(array('hack'=>0));
			exit;
		}
	}
	if($_POST['execute']==3){ // mo bao li xi
		global $wpdb;
	    $tb = 'ap_painting_play' ;
	    $mobaolixi = $wpdb->get_row("SELECT * FROM ap_painting_play WHERE id = {$_SESSION['mobaolixi']}");
	    $giaithuong = empty($mobaolixi->giaithuong)?array():unserialize($mobaolixi->giaithuong);
	    if(count($giaithuong)>=$_SESSION['baolixi']){
	    	echo json_encode(array('hack'=>1));
			exit;
	    }else{
	    	$random = rand(0,100);
	    	$data['hack'] = 0;
	    	if($random<=$ratio_mobaolixi[0]) {// trung giai LG
	    		// $data['award'] = 1;
	    		$data['award'] = 2;
	    		$code =  $mobaolixi->id.substr(md5(time()), 0, 8); 
			}else if($random>$ratio_mobaolixi[0]&&$random<=$ratio_mobaolixi[1]){// trung the cao
	    		$data['award'] = 2;
	    		$code =  $mobaolixi->id.substr(md5(time()), 0, 8); 
			}else{// trat
	    		$data['award'] = 3;
	    		if($mobaolixi->code==0) $code = 0;
	    		else $code = $mobaolixi->code;
			}
			$giaithuong[] = $data;
			$request = $mobaolixi->request;
			$request = array_merge(unserialize($request),$_POST);
		    $datadb = array(
		    	'giaithuong'=> serialize($giaithuong),
		    	'adddate'=> time(),
		    	'code'=> $code,
		    	'request'=> serialize($request),
		    	);
		    $wpdb->update($tb,$datadb,array( 'id' => $_SESSION['mobaolixi'] ));

		    $lastest_play = $wpdb->get_row("SELECT * FROM ap_painting_play WHERE user_id = '{$_SESSION['user_id']}' AND email!='' ORDER BY id DESC");
		    if(!empty($lastest_play)){
			    $data['username'] = $lastest_play->username;
			    $data['phone'] = $lastest_play->phone;
			    $data['cmnd'] = $lastest_play->cmnd;
			    $data['email'] = $lastest_play->email;
			}else{
				$data['username'] = '';
			    $data['phone'] = '';
			    $data['cmnd'] = '';
			    $data['email'] = '';
			}
			echo json_encode($data);
			exit;
	    }
	}
	if($_POST['execute']==4){ // luu thong tin va send mail
		global $wpdb;
	    $tb = 'ap_painting_play' ;
	    $mobaolixi = $wpdb->get_row("SELECT * FROM ap_painting_play WHERE id = {$_SESSION['mobaolixi']}");
	    $request = $mobaolixi->request;
		$request = array_merge(unserialize($request),$_POST);

	    $datadb = array(
	    	'username'=> $_POST['username'],
	    	'phone'=> $_POST['phone'],
	    	'cmnd'=> $_POST['cmnd'],
	    	'email'=> $_POST['email'],
	    	'request'=> serialize($request),
	    	);
	    $wpdb->update($tb,$datadb,array( 'id' => $_SESSION['mobaolixi'] ));
	    if($mobaolixi->code!=0){
			$toemail = $_POST['email'];
			$subject = 'Code nhận thưởng từ ĐẬP NIÊU FEEL LỘC'; 
			$message = "Code nhận thưởng của bạn: {$mobaolixi->code}";
			add_filter( 'wp_mail_content_type', 'set_html_content_type' );
			wp_mail( $toemail, $subject , $message );
			remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
		}
		exit;
	}
}

function set_html_content_type() {
	return 'text/html';
}

?>